<template>
  <!-- 主容器 -->
  <el-container class="main-class">
    <!-- 头部信息 -->
    <el-header>
      <el-row style="height: 100%">
        <!-- 头像 -->
        <el-col :span="2" style="height: 100%">
          <el-avatar :size="60" :src="avatar.startsWith('https') ? avatar: this.$qiniu + avatar"/>
        </el-col>
        <!-- 名称 -->
        <el-col :span="15" class="title">
          社区生活平台 <span>——管理员({{ name }})</span>
        </el-col>
        <!-- 退出 -->
        <el-col :span="7" class="logout">
          <el-button type="danger" @click="logout" round>退出登录</el-button>
        </el-col>
      </el-row>
    </el-header>
    <!-- 主体容器 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="200px" :width="menuActive ? '200px':'60px'">
        <div class="menuButton" @click="menuActive = !menuActive">
          <i class="el-icon-s-fold"></i>
        </div>
      <el-menu
        :default-active="activePath"
        :collapse="!menuActive"
        text-color="#fff"
        background-color="#222222"
        unique-opened
        router
        class="el-menu-vertical-demo">
        <el-submenu :index="index + ''" v-for="(parentMenu, index) in menus" :key="index">
          <template slot="title">
            <i :class="parentMenu.icon"/>
            <span>{{parentMenu.title}}</span>
          </template>
            <el-menu-item :index="childrenMenu.path" v-for="(childrenMenu,i) in parentMenu.children" :key="i" @click="savePath(childrenMenu.path)">
              <template slot="title">
                <i :class="childrenMenu.icon"/>
                <span>{{ childrenMenu.title }}</span>
              </template>
            </el-menu-item>
        </el-submenu>
      </el-menu>
      </el-aside>
      <!-- 主体 -->
      <el-main>
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item v-for="(item, index) in $router.currentRoute.matched" :key="index">{{ item.meta.title }}</el-breadcrumb-item>
        </el-breadcrumb>
        <span v-show="$router.currentRoute.path === '/'" class="main-title">欢迎来到社区生活平台</span>
        <router-view/>
      </el-main>
    </el-container>
  </el-container>
</template>




<script>
  import { mapState } from 'vuex';

  export default {
    computed: {
      ...mapState(['name','avatar','menus'])
    },
    data(){
      return {
        // 是否展开菜单
        menuActive: true,
        // 激活菜单
        activePath: sessionStorage.getItem('activePath'),
      }
    },

    methods: {
      logout() {
        this.$confirm('您将退出登录, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            // 调出退出接口
            this.$ajax.get('/user/logout').then((res) => {
              // 清空本地缓存
              sessionStorage.clear();
              // 跳转到登录界面
              this.$router.replace('/login');
              this.$message.success(res.data.message);
              window.location.reload();
          });
        }).catch(() => {

        });
      },
      /* 保存被激活的目录 */
      savePath() {
        sessionStorage.setItem('activePath',path);
        this.activePath = path;
      }
    }
  }
</script>

<style scoped>
  /* scoped受保护的样式，当前style标签下的样式只能在当前组件中生效，其他组件无法使用，避免样式污染 */

  .el-header, .el-footer {
    background-color: #567ebd;
    color: #333;
    text-align: left;
    line-height: 60px;
  }

  .el-aside {
    background-color: #444444;
    color: #333;
  }

  .el-main {
    background-color: #E9EEF3;
    color: #333;
    text-align: center;
  }

  .main-class {
    height: 100%;
  }

  .title {
    text-align: left;
    font-size: 25px;
    font-family: 黑体;
  }


  .logout {
    text-align: right;
  }

  .menuButton {
    font-size: 20px;
    background: #334455;
    color: white;
    cursor: pointer;

  }

  .main-title {
    font-size: 50px;
    font-family: 华文楷体;
  }

  .el-menu-item {
    background-color: #ffffff;
  }

  .el-submenu .el-menu-item {
    padding: 0;
  }
</style>
